<?php
if (strcmp($_GET['accion'], "Actualizar") == 0) {
?>
    <script>
        $("#nombre").val('<?php echo $_GET['nombre'] ?>');
        $("#apellido").val('<?php echo $_GET['apellido'] ?>');
        $("#cedula").val('<?php echo $_GET['cedula'] ?>');
        $("#telefono").val('<?php echo $_GET['telefono'] ?>');
        $("#direccion").val('<?php echo $_GET['direccion'] ?>');
        $("#email").val('<?php echo $_GET['email'] ?>');
        $("#nombre").val('<?php echo $_GET['nombre'] ?>');
        
    </script>
<?php
}
?>

<!--Fin Mensaje de error y exito-->

<div class="hero-unit">
    <form action="" id="personal-form" class="form-horizontal">
        <fieldset>
            <legend><?php echo $_GET['accion'] ?> personal</legend>

            <div class="control-group">
                <label class="control-label" for="">Nombres</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" name="nombre" id="nombre">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="">Apellidos</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" name="apellido" id="apellido">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="">Cedula</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" name="cedula" id="cedula">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="">Telefono</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" name="telefono" id="telefono">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="message">Direccion</label>
                <div class="controls">
                    <textarea class="input-xlarge" name="direccion" id="direccion" rows="3"></textarea>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="">Correo</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" name="email" id="email">
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="sexo">Sexo</label>
                <div class="controls">
                    <select  class="input-xlarge" name="sexo" id="sexo">
                        <option value="F">Femenino</option>
                        <option value="M">Masculino</option>
                    </select>   
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="">Cargo</label>
                <div class="controls">
                    <select  class="input-xlarge" name="cargo" id="cargo">
                        <option value="1">Obrero</option>
                        <option value="2">Administrativo</option>
                        <option value="3">Docente</option>
                    </select>   
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" data-loading-text="procesando..." data-complete-text="completo!" id="enviar" class="btn btn-primary">Guardar</button>
            </div>
        </fieldset>
    </form>
    <script>
        $('#personal-form').validate(
                {
                    rules: {
                        nombre: {
                            minlength: 1,
                            required: true,
                            letras: true
                        },
                        apellido: {
                            required: true,
                            minlength: 1,
                            letras: true
                        },
                        cedula: {
                            minlength: 1,
                            required: true,
                            cedula: true
                        },
                        direccion: {
                            minlength: 1,
                            required: true
                        },
                        telefono: {
                            minlength: 1,
                            required: true,
                            telefono: true
                        },
                        email: {
                            required: true,
                            email: true
                        },
                        sexo: {
                            required: true
                        },
                        cargo: {
                            required: true
                        }
                    },
                    highlight: function(element) {
                        $(element).closest('.control-group').removeClass('success').addClass('error');
                    },
                    success: function(element) {
                        element.text('OK!').addClass('valid').closest('.control-group').removeClass('error').addClass('success');
                    },
                    submitHandler: function(form) {
                        enviar(form);
                        $("#enviar").button('loading');
                    }
                });
        function enviar(form) {
            var valores = "";
            var url = "";
            var formJson = arrayToJson($(form).serializeArray());
            if ('<?php echo $_GET['accion'] ?>' === "Registrar") {
                valores = "('" + formJson.cedula + "','" + formJson.nombre + "','" + formJson.apellido + "','" + formJson.sexo + "','" + formJson.direccion + "','" + formJson.telefono + "','" + formJson.email + "','" + formJson.cargo + "')";
                url = './mysql/query.php?query=' + 'insertar_personal&valores=' + valores;
            }
            if ('<?php echo $_GET['accion'] ?>' === "Actualizar") {
                valores = "cedula='" + formJson.cedula + "', " + "nombre='" + formJson.nombre + "'," + "apellido='" + formJson.apellido + "'," + "sexo='" + formJson.sexo + "'," + "direccion='" + formJson.direccion + "'," + "telefono='" + formJson.telefono + "'," + "email='" + formJson.email + "'," + "cargo_id_cargo='" + formJson.cargo + "'";
                url = './mysql/query.php?query=' + 'update_personal&valores=' + valores + "&id_personal=" +"'"+ formJson.cedula+"'"
            }
            $.ajax({
                url: url,
                data: null,
                dataType: 'json',
                success: function(data)
                {
                    console.log("el resultado de : " + $.parseJSON(data).result)

                    if ($.parseJSON(data).result === 1) {
                        $("#enviar").button('complete');
                        $("#exito").removeClass("hidden")
                        setTimeout("jQuery('#exito').addClass('hidden')", 90000);
                        personal();
                    } else {
                        $("#enviar").button('complete');
                        $("#error").removeClass("hidden")
                        setTimeout("jQuery('#error').addClass('hidden')", 9000);
                        personal();
                    }


                },
                failure: function(response) {
                    console.log(response)
                    $("#error").addClass("hidden")
                    setTimeout("jQuery('#error').addClass('hidden')", 9000);
                },
                error: function(response) {
                    console.log(response)
                    $("#error").addClass("hidden")
                    setTimeout("jQuery('#error').addClass('hidden')", 9000);
                    if (response.status === 401) {
                        $("#content").html("Usuario no autorizado");
                    }

                }

            });
        }

    </script>
    <script src="js/libs/messages_es.js"></script>  
    <script src="js/libs/valid.js"></script> 
